#import "sprite_game_time_bar.h"

#import "mgr_game_mission.h"

#define MISSION_TIME 180.0f /*10.0f*/

@implementation SpriteGameTimeBar

- (id)initSpriteGameTimeBar
{
	if ((self = [super init]))
	{
		frame_ = [CCSprite spriteWithSpriteFrameName:@"hud_time.png"];
		inside_ = [CCProgressTimer progressWithFile:@"time_bar_progress.png"];
		inside_.type = kCCProgressTimerTypeHorizontalBarRL;
		inside_.percentage = 100;
		[self addChild:inside_];
		[self addChild:frame_];
	}
	return self;
}

- (void)MissionStart
{
	CCSequence *_Seq;
	id _ProcessAction;
	CCCallFuncN *_CallBackTimeUp;

	_ProcessAction = [CCProgressFromTo actionWithDuration:MISSION_TIME from:100 to:0];
	_CallBackTimeUp = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackTimeUp:)];
	_Seq = [CCSequence actions:_ProcessAction, _CallBackTimeUp, nil];
	[inside_ runAction:_Seq];
}

- (void)MissionOver
{
	[inside_ stopAllActions];
}

- (void)Pause
{
	[self pauseSchedulerAndActions];
	[inside_ pauseSchedulerAndActions];
}

- (void)Resume
{
	[self resumeSchedulerAndActions];
	[inside_ resumeSchedulerAndActions];
}

- (void)AddTime:(float)Time
{
	float _Persent;
	float _Time;

	CCSequence *_Seq;
	id _ProcessAction;
	CCCallFuncN *_CallBackTimeUp;

	[inside_ stopAllActions];

	/*time
	*/
	_Time = (inside_.percentage / 100.0f) * MISSION_TIME;
	_Time += Time;
	if (_Time > MISSION_TIME)
	{
		_Time = MISSION_TIME;
	}

	/*persent
	*/
	_Persent = _Time / MISSION_TIME * 100;

	/*action
	*/
	_ProcessAction = [CCProgressFromTo actionWithDuration:_Time from:_Persent to:0];
	_CallBackTimeUp = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackTimeUp:)];
	_Seq = [CCSequence actions:_ProcessAction, _CallBackTimeUp, nil];
	[inside_ runAction:_Seq];
}

- (void)ActionMissionStart
{
	CCSequence *_Seq;
	id _ProcessAction;
	CCCallFuncN *_CallBackActionOver;

	[frame_ runAction:[CCFadeIn actionWithDuration:MISSION_START_PLAY_TIME]];
	_ProcessAction = [CCProgressFromTo actionWithDuration:MISSION_START_PLAY_TIME from:0 to:100];
	_CallBackActionOver = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackActionMissionStartOver:)];
	_Seq = [CCSequence actions:_ProcessAction, _CallBackActionOver, nil];
	[inside_ runAction:_Seq];
}

- (void)CallBackActionMissionStartOver:(id)Sender
{
	CCSequence *_Seq;
	id _ProcessAction;
	CCCallFuncN *_CallBackTimeUp;

	_ProcessAction = [CCProgressFromTo actionWithDuration:MISSION_TIME from:100 to:0];
	_CallBackTimeUp = [CCCallFuncN actionWithTarget:self selector:@selector(CallBackTimeUp:)];
	_Seq = [CCSequence actions:_ProcessAction, _CallBackTimeUp, nil];
	[inside_ runAction:_Seq];
}

- (void)CallBackTimeUp:(id)Sender
{
	CMgrGameMission::Instance()->MissionTimeUp();
}

@end